package com.example.rabc.service;

import com.example.rabc.entity.Permission;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * 权限树缓存管理器
 * 实现权限树的缓存优化，提高权限验证的性能
 */
@Component
public class PermissionTreeCacheManager {
    
    // 权限树缓存
    private final ConcurrentHashMap<String, PermissionComponent> permissionTreeCache = new ConcurrentHashMap<>();
    
    // 用户权限树缓存
    private final ConcurrentHashMap<Long, List<PermissionComponent>> userPermissionTreeCache = new ConcurrentHashMap<>();
    
    // 缓存读写锁
    private final ReadWriteLock cacheLock = new ReentrantReadWriteLock();
    
    /**
     * 获取权限树缓存
     * @param cacheKey 缓存键
     * @return 权限树根节点
     */
    public PermissionComponent getPermissionTreeCache(String cacheKey) {
        cacheLock.readLock().lock();
        try {
            return permissionTreeCache.get(cacheKey);
        } finally {
            cacheLock.readLock().unlock();
        }
    }
    
    /**
     * 设置权限树缓存
     * @param cacheKey 缓存键
     * @param permissionTree 权限树根节点
     */
    public void setPermissionTreeCache(String cacheKey, PermissionComponent permissionTree) {
        cacheLock.writeLock().lock();
        try {
            permissionTreeCache.put(cacheKey, permissionTree);
        } finally {
            cacheLock.writeLock().unlock();
        }
    }
    
    /**
     * 获取用户权限树缓存
     * @param userId 用户ID
     * @return 用户权限树
     */
    public List<PermissionComponent> getUserPermissionTreeCache(Long userId) {
        cacheLock.readLock().lock();
        try {
            return userPermissionTreeCache.get(userId);
        } finally {
            cacheLock.readLock().unlock();
        }
    }
    
    /**
     * 设置用户权限树缓存
     * @param userId 用户ID
     * @param userPermissionTree 用户权限树
     */
    public void setUserPermissionTreeCache(Long userId, List<PermissionComponent> userPermissionTree) {
        cacheLock.writeLock().lock();
        try {
            userPermissionTreeCache.put(userId, userPermissionTree);
        } finally {
            cacheLock.writeLock().unlock();
        }
    }
    
    /**
     * 清除权限树缓存
     */
    public void clearPermissionTreeCache() {
        cacheLock.writeLock().lock();
        try {
            permissionTreeCache.clear();
        } finally {
            cacheLock.writeLock().unlock();
        }
    }
    
    /**
     * 清除指定用户的权限树缓存
     * @param userId 用户ID
     */
    public void clearUserPermissionTreeCache(Long userId) {
        cacheLock.writeLock().lock();
        try {
            userPermissionTreeCache.remove(userId);
        } finally {
            cacheLock.writeLock().unlock();
        }
    }
    
    /**
     * 当权限发生变化时调用此方法，清除相关缓存
     * @param permission 变更的权限
     */
    public void onPermissionChanged(Permission permission) {
        // 清除所有权限树缓存，因为权限变化可能影响整个树结构
        clearPermissionTreeCache();
        
        // 如果需要更精细的缓存管理，可以只清除受影响的用户缓存
        // 这里简化处理，清除所有用户权限树缓存
        cacheLock.writeLock().lock();
        try {
            userPermissionTreeCache.clear();
        } finally {
            cacheLock.writeLock().unlock();
        }
    }
}